package com.iman.sumer.web.space;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.iman.common.util.IdCreater;
import com.iman.sumer.service.teamgroup.TeamGroupService;
import com.iman.sumer.util.LinkMessages;
import com.iman.sumer.vo.teamgroup.TeamGroupVo;
import com.iman.sumer.web.BaseController;
import com.iman.sumer.web.teamgroup.TeamGroupController;

 
/**
 *  职业规划
 */
@Controller
@RequestMapping(value="/sumer/group/*")
public class GroupController extends BaseController{
	
	//日志
	private static final Logger logger = LoggerFactory
				.getLogger(TeamGroupController.class);
 
	//群组
	@Autowired
	private TeamGroupService teamGroupService;
	/**
	 * 
	 *  Function:
	 *  功能说明：   初始查询页面
	 *	 使用说明：
	 *  @author  wangxiaolei  DateTime 2014年12月18日 上午12:01:51
	 *	返回类型: ModelAndView    
	 *  @param request
	 *  @return
	 */
	@RequestMapping(value="/init")
	public ModelAndView init(HttpServletRequest request,String showid){	
		Map<String,Object> model = new HashMap<String,Object>();
		 
		logger.info("[个人群组管理]管理页面加载数据开始...");
		Map params = new HashMap();
		Long user_id = this.getUserIdInSession(request);
		
		//如果识别的用户进来了，是ID ，那么就是获取别人的内容，不能又添加修改操作
		
		if(showid!=null && NumberUtils.isDigits(showid )){
			user_id = NumberUtils.toLong(showid);
		}
		params.put("member_id", user_id);
		
		if(showid == null){
			showid = user_id.toString();
		}

		List<TeamGroupVo> data = teamGroupService.selfGroupManage(params);
		model.put("data", data);
		//总页数
		model.put("user_id", user_id);
		ModelAndView view = new ModelAndView("space/group",model);
		
		//  加入 给前台 是否是自己的数据 ，和  传递 showId   传递使用
		boolean isme = super.isMeData(request, showid);
		
		super.putShowId(view, user_id, isme);
		
		view.addObject("showImgURL", LinkMessages.getString("SHOW_IMG_URL"));	
		 
		//每个做后都要加上这个
		putResourceSrc(view ,request );
		logger.info("[个人群组管理]管理页面加载数据结束...");
		//点击tab颜色
		view.addObject("tabstatus" , "group");
		return view;
	}
	
	
	
	
	@RequestMapping(value="/init/{showid}")
	public ModelAndView init2(HttpServletRequest request,@PathVariable String showid){	
		Map<String,Object> model = new HashMap<String,Object>();
		Long userId = null;
		if(NumberUtils.isNumber( showid) ){
			userId = NumberUtils.toLong(showid);
		}
	 
		ModelAndView view = new ModelAndView("space/group",model);
		
		// 调用内部数据 查询
		view = init(  request,  showid); 
		 
		//每个做后都要加上这个
		putResourceSrc(view ,request );
		//点击tab颜色
		view.addObject("tabstatus" , "group");
		return view;
	}
	
	
	/**
	 * 跳转到 新增群组页面
	 * @return
	 */
	@RequestMapping(value="/group_add_page")
	@ResponseBody
	public ModelAndView groupAddPage(HttpServletRequest request){
		 logger.info("[新增群组活动]跳转到新增群组活动开始...");
		Map model = new HashMap();
		Map<String,Object> params = new HashMap<String,Object>();
		List<Map> list = teamGroupService.getGroupType();
		model.put("typeList", list);
		ModelAndView view = new ModelAndView("space/group_add",model);
		//每个做后都要加上这个
		putResourceSrc(view ,request );
		logger.info("[新增群组活动]跳转到新增群组活动结束...");
		return view;
	}
	
	/**
	 * 新增群组
	 * @return
	 */
	@RequestMapping(value="/group_add",method=RequestMethod.POST)
	@ResponseBody
	public ModelAndView groupAdd(HttpServletRequest request){
		 logger.info("[新增群组活动]新增群组活动开始...");
		Long create_user_id = this.getUserIdInSession(request);
		Map model = new HashMap();
		String group_name = request.getParameter("group_name");
		String group_txt = request.getParameter("group_txt");
		String group_img = request.getParameter("group_img");
		String location_nm = request.getParameter("location_nm");
		String group_notice = request.getParameter("group_notice");
		String class_tp = request.getParameter("class_tp");
		
		try {
			Map params = new HashMap();
			
			params.put("group_id", IdCreater.getId());
			params.put("group_name", group_name);
			params.put("group_txt", group_txt);
			params.put("group_img", group_img);
			params.put("location_nm", location_nm);
			params.put("group_notice", group_notice);
			params.put("create_id", create_user_id);
			params.put("class_tp", class_tp);
			teamGroupService.addGroup(params);
			//插入数据
			model.put("result", "success");
		} catch (Exception e) {
			e.printStackTrace();
			model.put("result", "error:"+e.getStackTrace());
		}
		ModelAndView view = new ModelAndView("teamgroup/result",model);
		super.putShowId(view, create_user_id, true);
		//每个做后都要加上这个
		putResourceSrc(view ,request );
		 logger.info("[新增群组活动]新增群组活动结束...");
		return view;
	}
	
	
	/**
	 * 退出群组
	 * @return
	 */
	@RequestMapping(value="/exit_group/{group_id}",method=RequestMethod.GET)
	@ResponseBody
	public ModelAndView exitGroup(HttpServletRequest request,String group_id){
		 logger.info("[新增群组活动]新增群组活动开始...");
		Long create_user_id = this.getUserIdInSession(request);
		Map model = new HashMap();
		
		try {
			Map params = new HashMap();
			params.put("group_id", group_id);
			params.put("user_id", create_user_id);
			teamGroupService.exitGroup(params);
			//插入数据
			model.put("result", "success");
		} catch (Exception e) {
			e.printStackTrace();
			model.put("result", "error:"+e.getStackTrace());
		}
		ModelAndView view = new ModelAndView("teamgroup/result",model);
		super.putShowId(view, create_user_id, true);
		//每个做后都要加上这个
		putResourceSrc(view ,request );
		 logger.info("[新增群组活动]新增群组活动结束...");
		return view;
	}
/******************************************************************************************************************************/
	
	 
	 
}
